package com.pesking.core.sqlparser.processor;

import java.util.Arrays;
import java.util.List;

import com.pesking.core.exception.SystemException;
import com.pesking.core.parameter.Parameter;
import com.pesking.core.sqlparser.parameter.SqlParameter;
import com.pesking.core.sqlparser.parameter.SqlParameterProcessor;
import com.pesking.core.util.StringUtils;

public class ParameterLengthSqlParameterHandler extends SqlParameterProcessor {
	@Override
	public String getTitle() {
		return "参数长度参数";
	}

	@Override
	public String getDescription() {
		return this.getTitle() + " 根据指定参数取得的参数值数组长度";
	}

	@Override
	public void handle(StringBuilder sbSQL, Parameter parameter, List<SqlParameter> processors,SqlParameter sqlParameter) {
		String name = sqlParameter.getName();
		if (StringUtils.isBlank(name)){
			throw new SystemException("获取参数数组长度,使用规则[PL:参数名]");
		}
		String []arr = parameter.getObjectParameterValues(name);
		int len = (arr == null ? 0 : arr.length);
		sbSQL.append(len);
	}

	@Override
	public List<String> getSqlParameterType() {
		return Arrays.asList(new String []{"PL","PARAMETER-LENGTH"});
	}
}
